import Session from '../dao/Session'

/**
 * 数据存储工厂
 */

class DaoFactory {

    private _engineInstance = {};

    /**
     * 
     * @param engine 
     * @param option  配置参数
     * @param singleton 
     */
    createDao(engine: string, singleton: boolean = true): Session {
        let session = null;
        if (singleton) {
            session = this._engineInstance[engine] || null;
            //没有，则加载
            if (!session) {
                session = this._queryDao(engine)
            }
        } else {
            session = this._queryDao(engine)
        }

        return session;
    }

    private _queryDao(engine: string) {
        let s = require('../dao/' + engine.toLowerCase() + '/Engine').default;
        return this._engineInstance[engine] = new s();
    }
}
let _daoFactory = new DaoFactory();
export default _daoFactory;